﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TLCApp
{
    public partial class Frm_Query : Form
    {
        private DataTable ListDt = new DataTable();
        public DataGridViewRow mSelectRow = new DataGridViewRow();
        private string mhtbh = "";
        /// <summary>
        /// 传入值
        /// </summary>
        public string strwhere
        {
            get { return _strwhere; }
            set
            {
                if (value == null)
                {
                    return;
                }
                _strwhere = value;
            }

        }
        private string _filter;
        /// <summary>
        /// 传入值过滤的字段|隔开;
        /// </summary>
        public string filter
        {
            get { return _filter; }
            set
            {
                if (value == null)
                {
                    return;
                }
                _filter = value;
            }

        }
        private string _strwhere;

        /// <summary>
        /// 全键盘操作
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Escape:

                    this.Close();

                    break;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
        public Frm_Query(DataTable Dt ,string htbh)
        {
            InitializeComponent();

            ListDt = Dt;
            mhtbh = htbh;
           ///设定宽带去掉左右滚动条
            if (Dt.Columns.Count > 8)
           {
           this.Width = Dt.Columns.Count * 60;
           }
            
              
        }

        private void Frm_Query_Load(object sender, EventArgs e)
        {
           
            txtBMMC.Text = strwhere;
            query(strwhere);
        }

        private void dgvList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
            else if (e.KeyCode==Keys.Enter)
            {
                mSelectRow = dgvList.CurrentRow;
                this.Close();
            }
        }

        private void Frm_Query_Activated(object sender, EventArgs e)
        {
            dgvList.Focus();
        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            mSelectRow = dgvList.CurrentRow;
            this.Close();
        }

        private void Frm_Query_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                this.Close();
        }

        private void txtBMMC_TextChanged(object sender, EventArgs e)
        {
            if (chKht.Checked)
            {
                query(txtBMMC.Text.Trim(), mhtbh);
            }
            else
            {
                query(txtBMMC.Text.Trim());
            }

        }
        void query(string strFilter, string htbh)
        {
            if (ListDt == null || filter == "")
            {
                mSelectRow = null;
                this.Close();

            }
            string strquery = " ";
            //----处理条件--
            string[] strArrfiter = filter.Split('|');
           if (strArrfiter.Contains("合同编号"))
           {
               strquery = string.Format("  合同编号 = '{0}'", htbh);
           }

            if (chkzncx.Checked)
            {
                for (int i = 0; i < strArrfiter.Length; i++)
                {

                    string format = string.Format(" " + strArrfiter[i] + " like '%{0}%'", strFilter);
                    //strquery = strquery + format;
                    if ((i != 0))
                    {
                        strquery = strquery + " or " + format;
                    }
                    else
                    {
                        strquery = strquery +"and  ("+ format;
                    }

                }
                strquery = strquery + ")";
            }
            else
            {
                for (int i = 0; i < strArrfiter.Length; i++)
                {

                    string format = string.Format(" " + strArrfiter[i] + " like '{0}%'", strFilter);
                    //strquery = strquery + format;
                    if ((i != 0))
                    {
                        strquery = strquery + " or " + format;
                    }
                    else
                    {
                        strquery = strquery + "and  (" + format;
                    }

                }
                strquery = strquery + ")";

            }
            //-----------
            DataView dv = ListDt.Copy().DefaultView;
            dv.RowFilter = strquery;
            this.dgvList.DataSource = dv.ToTable();
   
        
        }
        void query(string strFilter)
        {
            if (ListDt == null || filter == "")
            {
                mSelectRow = null;
                this.Close();

            }
            string strquery = " ";
            //----处理条件--
            string[] strArrfiter = filter.Split('|');
         
            if (chkzncx.Checked)
            {
                for (int i = 0; i < strArrfiter.Length; i++)
                {

                    string format = string.Format(" " + strArrfiter[i] + " like '%{0}%'", strFilter);
                    //strquery = strquery + format;
                    if ((i != 0))
                    {
                        strquery = strquery + " or " + format;
                    }
                    else
                    {
                        strquery = strquery + format;
                    }

                }
            }
            else
            {
                for (int i = 0; i < strArrfiter.Length; i++)
                {

                    string format = string.Format(" " + strArrfiter[i] + " like '{0}%'", strFilter);
                    //strquery = strquery + format;
                    if ((i != 0))
                    {
                        strquery = strquery + " or " + format;
                    }
                    else
                    {
                        strquery = strquery + format;
                    }

                }

            }
            //-----------
            DataView dv = ListDt.Copy().DefaultView;
            if (strFilter.Length > 0)
            {
                dv.RowFilter = strquery;
            }
            this.dgvList.DataSource = dv.ToTable();
   
        }

        private void chKht_CheckedChanged(object sender, EventArgs e)
        {
            if (chKht.Checked)
            {
                query(txtBMMC.Text.Trim(), mhtbh);
            }
            else
            {
                query(txtBMMC.Text.Trim());
            }
        }

        private void chkzncx_CheckedChanged(object sender, EventArgs e)
        {
            if (chKht.Checked)
            {
                query(txtBMMC.Text.Trim(), mhtbh);
            }
            else
            {
                query(txtBMMC.Text.Trim());
            }
        }

    }
}
